subroutine denomt1_sg(t1, r1, ie, sym)
!use common 
use mod_symm
use mod_iop
use mod_size
use mod_orbit
use mod_ioff 
implicit none
integer, intent(in) :: sym
real*4, intent(inout),device :: t1(*), r1(*), ie(*)
!
integer i, j, a, b, idx, iofft
integer irrepi, irrepa, numa, numi
integer ioffoa(8), ioffva(8) 
integer ioffob(8), ioffvb(8) 
integer, external :: irpdso, isymoffso
real*8 dtmp
!
ioffoa(1) = 0; ioffob(1) = 0
do irrepi = 1, nirrep - 1
   ioffoa(irrepi + 1) = ioffoa(irrepi) + popa(irrepi) 
   ioffob(irrepi + 1) = ioffob(irrepi) + popb(irrepi) 
enddo
!
ioffva(1) = ioffoa(nirrep) + popa(nirrep)
ioffvb(1) = ioffob(nirrep) + popb(nirrep)
do irrepi = 1, nirrep - 1
   ioffva(irrepi + 1) = ioffva(irrepi) + vrta(irrepi) 
   ioffvb(irrepi + 1) = ioffvb(irrepi) + vrtb(irrepi) 
enddo
!
do irrepi = 1, nirrep
   irrepa = dirprd(irrepi, 1)
!---for AA spin
   numa = vrta(irrepa)
   numi = popa(irrepi)
   iofft = isymoffso(irrepi, 1, vrta, popa, 1) - 1
!$acc kernels
!$acc loop independent
   do i = 1, numi 
!$acc loop independent
      do a = 1, numa 
           dtmp = ie(ioffoa(irrepi) + i) - ie(ioffva(irrepa) + a)
           dtmp = 1.d0/dtmp
           idx = iofft + (i-1)*numa + a 
           t1(idx) = r1(idx)*dtmp
      enddo
   enddo
!$acc end kernels
!---for BB spin
   numa = vrtb(irrepa)
   numi = popb(irrepi)
   iofft = ifvo(2) - 1 + isymoffso(irrepi, 1, vrtb, popb, 1) - 1
!$acc kernels
!$acc loop independent
   do i = 1, numi
!$acc loop independent
      do a = 1, numa
           dtmp = ie(noa + nva + ioffob(irrepi) + i) & 
                - ie(noa + nva + ioffvb(irrepa) + a)
           dtmp = 1.d0/dtmp
           idx = iofft + (i-1)*numa + a
           t1(idx) = r1(idx)*dtmp
      enddo
   enddo
!$acc end kernels
!
enddo 
!
return 
end
 

subroutine denomt2_sg(t2, r2, ie, sym, iopab, iopij)
!use common 
use mod_symm
use mod_iop
use mod_size
use mod_orbit
use mod_ioff 
implicit none
integer, intent(in) :: sym, iopab, iopij
real*4, intent(inout),device :: t2(*), r2(*), ie(*)
!
integer i, j, a, b, irrepi, irrepj, irrepa, irrepb
integer numa, numb, numi, numj, iofft2(8) 
integer irrepab, irrepij, numab, numij, na
integer ioffoa(8), ioffva(8) 
integer ioffob(8), ioffvb(8) 
integer idxa, idxb, idxi, idxj, idx, iofft
integer, external :: irpdso, isymoffso, idsymsoc
real*8 dtmp
!
na = nva + noa
ioffoa(1) = 0; ioffob(1) = 0
do irrepi = 1, nirrep - 1
   ioffoa(irrepi + 1) = ioffoa(irrepi) + popa(irrepi) 
   ioffob(irrepi + 1) = ioffob(irrepi) + popb(irrepi) 
enddo
!
ioffva(1) = ioffoa(nirrep) + popa(nirrep)
ioffvb(1) = ioffob(nirrep) + popb(nirrep)
do irrepi = 1, nirrep - 1
   ioffva(irrepi + 1) = ioffva(irrepi) + vrta(irrepi) 
   ioffvb(irrepi + 1) = ioffvb(irrepi) + vrtb(irrepi) 
enddo
!
!---------------for AAAA 
iofft2(1) = 1
do irrepij = 1, nirrep - 1
   irrepab = dirprd(irrepij, 1) 
   numij = irpdso(irrepij, popa, popa, 0) 
   numab = irpdso(irrepab, vrta, vrta, 0) 
   iofft2(irrepij+1) = iofft2(irrepij) + numab*numij
enddo 
!
do irrepij = 1, nirrep 
   irrepab = dirprd(irrepij, 1) 
   numab = irpdso(irrepab, vrta, vrta, 0)
   numij = irpdso(irrepij, popa, popa, 0)
   do irrepj = 1, nirrep 
      irrepi = dirprd(irrepij, irrepj) 
      numi = popa(irrepi)
      numj = popa(irrepj)
      if(irrepi>irrepj) cycle
      do irrepb = 1, nirrep
         irrepa = dirprd(irrepb, irrepab)
         if(irrepa>irrepb) cycle
         numa = vrta(irrepa) 
         numb = vrta(irrepb)
         iofft = iofft2(irrepij)-1 &
              + (isymoffso(irrepj,irrepij,popa,popa,0)-1)*numab & 
              +  isymoffso(irrepb,irrepab,vrta,vrta,0)-1
         idxa = ioffva(irrepa)
         idxb = ioffva(irrepb) 
         idxi = ioffoa(irrepi)
         idxj = ioffoa(irrepj) 
         if(irrepj/=irrepi.and.irrepa/=irrepb) then 
!$acc kernels
!$acc loop independent
            do j = 1, numj
!$acc loop independent
            do i = 1, numi 
!$acc loop independent
            do b = 1, numb
!$acc loop independent
            do a = 1, numa 
               idx = iofft + ((j-1)*numi + i-1)*numab &
                          +  (b-1)*numa + a
               dtmp = ie(idxa+a)+ie(idxb+b)-ie(idxi+i)-ie(idxj+j) 
               dtmp = -1.d0/dtmp
               t2(idx) = r2(idx)*dtmp
            enddo
            enddo
            enddo
            enddo 
!$acc end kernels
         elseif(irrepj==irrepi.and.irrepa/=irrepb) then
!$acc kernels
!$acc loop independent
            do j = 2, numj
!$acc loop independent
            do i = 1, j-1 
!$acc loop independent
            do b = 1, numb
!$acc loop independent
            do a = 1, numa
               idx = iofft + ((j-1)*(j-2)/2 + i-1)*numab &
                          +  (b-1)*numa + a
               dtmp = ie(idxa+a)+ie(idxb+b)-ie(idxi+i)-ie(idxj+j)
               dtmp = -1.d0/dtmp
               t2(idx) = r2(idx)*dtmp
            enddo
            enddo
            enddo
            enddo
!$acc end kernels
         elseif(irrepj/=irrepi.and.irrepa==irrepb) then
!$acc kernels
!$acc loop independent
            do j = 1, numj
!$acc loop independent
            do i = 1, numi
!$acc loop independent
            do b = 2, numb
!$acc loop independent
            do a = 1, b-1
               idx = iofft + ((j-1)*numi + i-1)*numab &
                          +  (b-1)*(b-2)/2 + a
               dtmp = ie(idxa+a)+ie(idxb+b)-ie(idxi+i)-ie(idxj+j)
               dtmp = -1.d0/dtmp
               t2(idx) = r2(idx)*dtmp
            enddo
            enddo
            enddo
            enddo
!$acc end kernels
         elseif(irrepj==irrepi.and.irrepa==irrepb) then
!$acc kernels
!$acc loop independent
            do j = 2, numj
!$acc loop independent
            do i = 1, j-1
!$acc loop independent
            do b = 2, numb
!$acc loop independent
            do a = 1, b-1
               idx = iofft + ((j-1)*(j-2)/2 + i-1)*numab &
                          +  (b-1)*(b-2)/2 + a
               dtmp = ie(idxa+a)+ie(idxb+b)-ie(idxi+i)-ie(idxj+j)
               dtmp = -1.d0/dtmp
               t2(idx) = r2(idx)*dtmp
            enddo
            enddo
            enddo
            enddo
!$acc end kernels
         endif
      enddo
   enddo       
enddo
!
!---------------for BBBB 
iofft2(1) = 1
do irrepij = 1, nirrep - 1
   irrepab = dirprd(irrepij, 1)
   numij = irpdso(irrepij, popb, popb, 0)
   numab = irpdso(irrepab, vrtb, vrtb, 0)
   iofft2(irrepij+1) = iofft2(irrepij) + numab*numij
enddo
!
do irrepij = 1, nirrep
   irrepab = dirprd(irrepij, 1)
   numab = irpdso(irrepab, vrtb, vrtb, 0)
   numij = irpdso(irrepij, popb, popb, 0)
   do irrepj = 1, nirrep
      irrepi = dirprd(irrepij, irrepj)
      numi = popb(irrepi)
      numj = popb(irrepj)
      if(irrepi>irrepj) cycle
      do irrepb = 1, nirrep
         irrepa = dirprd(irrepb, irrepab)
         if(irrepa>irrepb) cycle
         numa = vrtb(irrepa)
         numb = vrtb(irrepb)
         iofft = ioi3(3) - 1 + iofft2(irrepij)-1 &
              + (isymoffso(irrepj,irrepij,popb,popb,0)-1)*numab &
              +  isymoffso(irrepb,irrepab,vrtb,vrtb,0)-1
         idxa = na + ioffvb(irrepa)
         idxb = na + ioffvb(irrepb)
         idxi = na + ioffob(irrepi)
         idxj = na + ioffob(irrepj)
         if(irrepj/=irrepi.and.irrepa/=irrepb) then
!$acc kernels
!$acc loop independent
            do j = 1, numj
!$acc loop independent
            do i = 1, numi
!$acc loop independent
            do b = 1, numb
!$acc loop independent
            do a = 1, numa
               idx = iofft + ((j-1)*numi + i-1)*numab &
                          +  (b-1)*numa + a
               dtmp = ie(idxa+a)+ie(idxb+b)-ie(idxi+i)-ie(idxj+j)
               dtmp = -1.d0/dtmp
               t2(idx) = r2(idx)*dtmp
            enddo
            enddo
            enddo
            enddo
!$acc end kernels
         elseif(irrepj==irrepi.and.irrepa/=irrepb) then
!$acc kernels
!$acc loop independent
            do j = 2, numj
!$acc loop independent
            do i = 1, j-1
!$acc loop independent
            do b = 1, numb
!$acc loop independent
            do a = 1, numa
               idx = iofft + ((j-1)*(j-2)/2 + i-1)*numab &
                          +  (b-1)*numa + a
               dtmp = ie(idxa+a)+ie(idxb+b)-ie(idxi+i)-ie(idxj+j)
               dtmp = -1.d0/dtmp
               t2(idx) = r2(idx)*dtmp
            enddo
            enddo
            enddo
            enddo
!$acc end kernels
         elseif(irrepj/=irrepi.and.irrepa==irrepb) then
!$acc kernels
!$acc loop independent
            do j = 1, numj
!$acc loop independent
            do i = 1, numi
!$acc loop independent
            do b = 2, numb
!$acc loop independent
            do a = 1, b-1
               idx = iofft + ((j-1)*numi + i-1)*numab &
                          +  (b-1)*(b-2)/2 + a
               dtmp = ie(idxa+a)+ie(idxb+b)-ie(idxi+i)-ie(idxj+j)
               dtmp = -1.d0/dtmp
               t2(idx) = r2(idx)*dtmp
            enddo
            enddo
            enddo
            enddo
!$acc end kernels
         elseif(irrepj==irrepi.and.irrepa==irrepb) then
!$acc kernels
!$acc loop independent
            do j = 2, numj
!$acc loop independent
            do i = 1, j-1
!$acc loop independent
            do b = 2, numb
!$acc loop independent
            do a = 1, b-1
               idx = iofft + ((j-1)*(j-2)/2 + i-1)*numab &
                          +  (b-1)*(b-2)/2 + a
               dtmp = ie(idxa+a)+ie(idxb+b)-ie(idxi+i)-ie(idxj+j)
               dtmp = -1.d0/dtmp
               t2(idx) = r2(idx)*dtmp
            enddo
            enddo
            enddo
            enddo
!$acc end kernels
         endif
      enddo
   enddo
enddo
!
!---------------for ABAB 
iofft2(1) = 1
do irrepij = 1, nirrep - 1
   irrepab = dirprd(irrepij, 1)
   numij = irpdso(irrepij, popa, popb, 1)
   numab = irpdso(irrepab, vrta, vrtb, 1)
   iofft2(irrepij+1) = iofft2(irrepij) + numab*numij
enddo
!
do irrepij = 1, nirrep
   irrepab = dirprd(irrepij, sym)
   numab = irpdso(irrepab, vrta, vrtb, 1)
   numij = irpdso(irrepij, popa, popb, 1)
   do irrepj = 1, nirrep
      irrepi = dirprd(irrepij, irrepj)
      numi = popa(irrepi)
      numj = popb(irrepj)
      do irrepb = 1, nirrep
         irrepa = dirprd(irrepb, irrepab)
         numa = vrta(irrepa)
         numb = vrtb(irrepb)
         iofft = ioi3(2)-1 +iofft2(irrepij)-1 &
              + (isymoffso(irrepj,irrepij,popa,popb,1)-1)*numab &
              +  isymoffso(irrepb,irrepab,vrta,vrtb,1)-1
         idxa = ioffva(irrepa)
         idxb = na + ioffvb(irrepb)
         idxi = ioffoa(irrepi)
         idxj = na + ioffob(irrepj)
!$acc kernels
!$acc loop independent
            do j = 1, numj
!$acc loop independent
            do i = 1, numi
!$acc loop independent
            do b = 1, numb
!$acc loop independent
            do a = 1, numa
               idx = iofft + ((j-1)*numi + i-1)*numab &
                          +  (b-1)*numa + a
               dtmp = ie(idxa+a)+ie(idxb+b)-ie(idxi+i)-ie(idxj+j)
               dtmp = -1.d0/dtmp
               t2(idx) = r2(idx)*dtmp
            enddo
            enddo
            enddo
            enddo
!$acc end kernels
      enddo
   enddo
enddo


return
end
